package com.test.rabbitmq.controller;

import com.test.rabbitmq.handler.ConfirmCallbackService;
import com.test.rabbitmq.handler.ReturnCallbackService;
import com.test.rabbitmq.po.Test;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/test")
public class SendController {
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @Autowired
    private ConfirmCallbackService confirmCallbackService;
    @Autowired
    private ReturnCallbackService returnCallbackService;
    /*@Autowired
    public SendController(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }*/

    @GetMapping
    public void defaultMessage(){
        Test test = new Test();
        test.setId(1);
        test.setName("11");
        test.setEmail("11111");
        /**
         * 确保消息发送失败可以重新返回队列中
         */
        rabbitTemplate.setMandatory(true);
        /**
         * 消费者确认收到消息后，手动ack回执回调处理
         * 消息投递到队列失败回调补偿处理
         */
        rabbitTemplate.setConfirmCallback(confirmCallbackService);
        rabbitTemplate.setReturnCallback(returnCallbackService);
        //todo direct routingKey 和 bindingKey全员匹配
        rabbitTemplate.convertAndSend("default_test_exchange","test.default",test);
        //这里需要手动ack
        rabbitTemplate.convertAndSend("manual_test_exchange","test.manual",test);

        // todo fanout 只需要制定交换机，消息会投递到所有的队列
        rabbitTemplate.convertAndSend("fanout_test_exchange","",test);

        //只有listenerTopicNote 可以监听到
        rabbitTemplate.convertAndSend("topic_test_exchange","topic.test.note",test);

        //两个都可以监听到  listenerTopicMessage   listenerTopicNote
        rabbitTemplate.convertAndSend("topic_test_exchange","topic.test.note",test);

        //info 可以被listenerRoutingError 和listenerRoutingInfo监听
        rabbitTemplate.convertAndSend("routing_test_exchange","info",test);

        // error 只能被listenerRoutingError 监听
        rabbitTemplate.convertAndSend("routing_test_exchange","error",test);

        //routingKey 直接指向某个队列  helloWord模式
        rabbitTemplate.convertAndSend("default_test_queue",test);
    }
}
